package cn.lixu.order.service.impl;

import cn.lixu.basic.service.impl.BaseServiceImpl;
import cn.lixu.order.domain.OrderPetAcquisition;
import cn.lixu.order.dto.OrderDto;
import cn.lixu.order.mapper.OrderPetAcquisitionMapper;
import cn.lixu.order.service.IOrderPetAcquisitionService;
import cn.lixu.org.mapper.EmployeeMapper;
import cn.lixu.org.mapper.ShopMapper;
import cn.lixu.pet.domain.Pet;
import cn.lixu.pet.mapper.PetMapper;
import cn.lixu.searchMaster.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;


    /**
     * 订单确认实现类
     *
     * @param dto
     */
    @Override
    public void confirm(OrderDto dto) {

        //将数据取出
        Long id = dto.getId();//订单编号
        BigDecimal money = dto.getMoney();//订单金额
        Integer payType = dto.getPayType();//订单支付方式
        //1.修改订单状态、金额、支付方式
        //查询订单
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        //修改状态
        orderPetAcquisition.setState(2);
        //修改金额
        orderPetAcquisition.setPrice(money);
        //修改支付方式
        orderPetAcquisition.setPaytype(payType);
        //修改订单数据到数据库
        orderPetAcquisitionMapper.update(orderPetAcquisition);

        //2.修改寻主消息状态
        searchMasterMsgMapper.updatState(orderPetAcquisition.getSearch_master_msg_id());

        //3.修改pet金额
        //获取pet对象
        System.out.println(orderPetAcquisition.getPet_id());
        Pet pet = petMapper.loadById(orderPetAcquisition.getPet_id());
        System.out.println(pet);
        //重新赋值
        pet.setCostprice(money);

        //保存到数据库
        petMapper.update(pet);


    }
}

